﻿using community.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace community.Controllers
{
    [Route("api/property-fees")]
    [ApiController]
    public class PropertyFeesController : ControllerBase
    {
        private static List<PropertyFee> propertyFees = new List<PropertyFee>();

        // 添加物业费用
        [HttpPost]
        public IActionResult AddPropertyFee([FromBody] PropertyFee fee)
        {
            fee.Id = propertyFees.Count + 1;
            fee.CreatedAt = DateTime.UtcNow;
            fee.UpdatedAt = DateTime.UtcNow;
            propertyFees.Add(fee);
            return CreatedAtAction(nameof(GetPropertyFeeById), new { id = fee.Id }, fee);
        }

        // 查询所有物业费用
        [HttpGet]
        public IActionResult GetAllPropertyFees()
        {
            return Ok(propertyFees);
        }

        // 根据ID查询物业费用
        [HttpGet("{id}")]
        public IActionResult GetPropertyFeeById(int id)
        {
            var fee = propertyFees.Find(f => f.Id == id);
            if (fee == null)
            {
                return NotFound();
            }
            return Ok(fee);
        }

        // 更新物业费用
        [HttpPut("{id}")]
        public IActionResult UpdatePropertyFee(int id, [FromBody] PropertyFee updatedFee)
        {
            var fee = propertyFees.Find(f => f.Id == id);
            if (fee == null)
            {
                return NotFound();
            }

            fee.HouseId = updatedFee.HouseId;
            fee.Amount = updatedFee.Amount;
            fee.DueDate = updatedFee.DueDate;
            fee.IsPaid = updatedFee.IsPaid;
            fee.UpdatedAt = DateTime.UtcNow;

            return NoContent();
        }

        // 删除物业费用
        [HttpDelete("{id}")]
        public IActionResult DeletePropertyFee(int id)
        {
            var fee = propertyFees.Find(f => f.Id == id);
            if (fee == null)
            {
                return NotFound();
            }

            propertyFees.Remove(fee);
            return NoContent();
        }
    }
}
